#define _CRT_SECURE_NO_WARNINGS
//dfs
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        dfs(root);
        return root;
    }
    void dfs(TreeNode* root) {
        if (root == nullptr) return;
        swap(root->left, root->right);
        if (root->left) dfs(root->left);
        if (root->right) dfs(root->right);
    }
};
//bfs
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr) return root;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            TreeNode* node = q.front();
            q.pop();
            swap(node->left, node->right);
            if (node->right) q.push(node->right);
            if (node->left) q.push(node->left);
        }
        return root;
    }
};